package com.mvp.shq.washer.ui.shop.order.ordersubmit;


import android.util.Log;

import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.shop.AddressData;
import com.mvp.shq.washer.entity.shop.TotalOrderData;
import com.mvp.shq.washer.entity.shop.result.OrderDataResult;
import com.mvp.shq.washer.entity.shop.result.OrderResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;

/**
 * Created by westing on 2017/10/11.
 */
public class Presenter extends OrderSumbitConstract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    void getDefAddress() {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        model.getDefaultAddress(token,userId)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<AddressData>(view) {
                    @Override
                    protected void onSuccees(BaseResult<AddressData> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getDefaultAddress(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void createOrder(TotalOrderData ecTotalOrderDto) {
        String token = SpUtil.getToken();
        model.createOrder(token,ecTotalOrderDto)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<OrderDataResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<OrderDataResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.createOrder(t.getData());
                        }
                    }
                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        Log.e("createMyOrder","onFailure："+e.getMessage());
                        e.printStackTrace();
                    }
                });
    }
}
